#!/bin/sh
OSS=`cat /proc/uptime | awk '{print $1}'`;OSS=${OSS%%.*};if [ $OSS -le 60 ];then sleep 30;fi

dev1="wl0";Threshold1=-80
dev2="wl1";Threshold2=-80
dev3="";Threshold3=-85

devHide1=`iwpriv $dev1 get_hide_ssid | sed 's/^.*d://g'`>&nul
devHide2=`iwpriv $dev2 get_hide_ssid | sed 's/^.*d://g'`>&nul
devHide3=`iwpriv $dev3 get_hide_ssid | sed 's/^.*d://g'`>&nul

MacCount=0;BMacTimeCount1=0;BMacTimeCount2=0;BMacTimeCount3=0

logfile="/tmp/Kicked.log"
if [ ! -f $logfile ];then
	echo [$(date +%F%n%T)]弱信号剔除记录文件已建立! 文件位置：$logfile
	echo [$(date +%F%n%T)]弱信号剔除记录文件已建立! >> $logfile
fi
while [ ture ];do
	if [ "$dev1" != "" ];then
		devSec=`iwpriv $dev1 get_maccmd_sec`;devSec=${devSec#*:}
		if [ $devSec != 1 ];then iwpriv $dev1 maccmd_sec 2;fi
		macs=`iwinfo $dev1 assoclist | grep ..:..:..:..:..:.. | grep dBm | awk '{print $1}' | wc -l`
		while [ $MacCount -lt $macs ];do
			MacCount=$((${MacCount}+1))
			mac=`iwinfo $dev1 assoclist | grep ..:..:..:..:..:.. | grep dBm | sed -n ${MacCount}p | awk '{print $1}'`
			rssi=`iwinfo $dev1 assoclist | grep ..:..:..:..:..:.. | grep dBm | sed -n ${MacCount}p | awk '{print $4}'`
			if [ $rssi -lt $Threshold1 ];then
				#if [ $devHide1 == 0 ];then iwpriv $dev1 hide_ssid 1;fi
				iwpriv $dev1 kickmac $mac
				echo [$(date +%F%n%T)]设备：`cat /tmp/dhcp.leases | grep -i $mac | awk '{print $4}'` IP: `cat /tmp/dhcp.leases | grep -i $mac | awk '{print $3}'` MAC：$mac 所在WIFI：`iwinfo $dev1 info | grep ESSID | sed 's/^.*: //g'` 信号强度：$rssi 低于已设置阈值 $Threshold1 ，已被踢出！ >> $logfile
				if [ $devSec == 2 ];then iwpriv $dev1 addmac_sec $mac
				else iwpriv $dev1 delmac_sec $mac;echo $mac >> "/tmp/${dev1}whitelist.tmp";fi
			fi
		done;MacCount=0
		if [ $devSec == 2 ];then bmacs=`iwpriv $dev1 getmac_sec`;bmacs=${bmacs#*:};else bmacs=`cat /tmp/${dev1}whitelist.tmp | head -n 1`>&nul;fi
		if [ "$bmacs" != "" ];then BMacTimeCount1=$((${BMacTimeCount1}+1));else BMacTimeCount1=0;fi
		if [ $BMacTimeCount1 == 11 ];then BMacTimeCount1=0
			#if [ $devHide1 == 0 ];then iwpriv $dev1 hide_ssid 0;fi
			if [ $devSec == 2 ];then
				while [ 0 -lt `iwpriv $dev1 getmac_sec | grep ..:..:..:..:..:.. | sed 's/^.*c://g' | wc -l` ];do
					bmacs=`iwpriv $dev1 getmac_sec | grep ..:..:..:..:..:.. | sed 's/^.*c://g' | sed -n 1p`
					iwpriv $dev1 delmac_sec $bmacs
				done
			else
				while [ 0 -lt `cat /tmp/${dev1}whitelist.tmp | wc -l` ];do
					bmacs=`cat /tmp/${dev1}whitelist.tmp | sed -n 1p`
					iwpriv $dev1 addmac_sec $bmacs
					sed -i '1d' /tmp/${dev1}whitelist.tmp
				done
				rm -Rf /tmp/${dev1}whitelist.tmp
			fi
		fi
	fi
	if [ "$dev2" != "" ];then
		devSec=`iwpriv $dev2 get_maccmd_sec`;devSec=${devSec#*:}
		if [ $devSec != 1 ];then iwpriv $dev2 maccmd_sec 2;fi
		macs=`iwinfo $dev2 assoclist | grep ..:..:..:..:..:.. | grep dBm | awk '{print $1}' | wc -l`
		while [ $MacCount -lt $macs ];do
			MacCount=$((${MacCount}+1))
			mac=`iwinfo $dev2 assoclist | grep ..:..:..:..:..:.. | grep dBm | sed -n ${MacCount}p | awk '{print $1}'`
			rssi=`iwinfo $dev2 assoclist | grep ..:..:..:..:..:.. | grep dBm | sed -n ${MacCount}p | awk '{print $4}'`
			if [ $rssi -lt $Threshold2 ];then
				#if [ $devHide2 == 0 ];then iwpriv $dev2 hide_ssid 1;fi
				iwpriv $dev2 kickmac $mac
				echo [$(date +%F%n%T)]设备：`cat /tmp/dhcp.leases | grep -i $mac | awk '{print $4}'` IP: `cat /tmp/dhcp.leases | grep -i $mac | awk '{print $3}'` MAC：$mac 所在WIFI：`iwinfo $dev2 info | grep ESSID | sed 's/^.*: //g'` 信号强度：$rssi 低于已设置阈值 $Threshold2 ，已被踢出！ >> $logfile
				if [ $devSec == 2 ];then iwpriv $dev2 addmac_sec $mac
				else iwpriv $dev2 delmac_sec $mac;echo $mac >> "/tmp/${dev2}whitelist.tmp";fi
			fi
		done;MacCount=0
		if [ $devSec == 2 ];then bmacs=`iwpriv $dev2 getmac_sec`;bmacs=${bmacs#*:};else bmacs=`cat /tmp/${dev2}whitelist.tmp | head -n 1`>&nul;fi
		if [ "$bmacs" != "" ];then BMacTimeCount2=$((${BMacTimeCount2}+1));else BMacTimeCount2=0;fi
		if [ $BMacTimeCount2 == 11 ];then BMacTimeCount2=0
			#if [ $devHide2 == 0 ];then iwpriv $dev2 hide_ssid 0;fi
			if [ $devSec == 2 ];then
				while [ 0 -lt `iwpriv $dev2 getmac_sec | grep ..:..:..:..:..:.. | sed 's/^.*c://g' | wc -l` ];do
					bmacs=`iwpriv $dev2 getmac_sec | grep ..:..:..:..:..:.. | sed 's/^.*c://g' | sed -n 1p`
					iwpriv $dev2 delmac_sec $bmacs
				done
			else
				while [ 0 -lt `cat /tmp/${dev2}whitelist.tmp | wc -l` ];do
					bmacs=`cat /tmp/${dev2}whitelist.tmp | sed -n 1p`
					iwpriv $dev2 addmac_sec $bmacs
					sed -i '1d' /tmp/${dev2}whitelist.tmp
				done
				rm -Rf /tmp/${dev2}whitelist.tmp
			fi
		fi
	fi
	if [ "$dev3" != "" ];then
		devSec=`iwpriv $dev3 get_maccmd_sec`;devSec=${devSec#*:}
		if [ $devSec != 1 ];then iwpriv $dev3 maccmd_sec 2;fi
		macs=`iwinfo $dev3 assoclist | grep ..:..:..:..:..:.. | grep dBm | awk '{print $1}' | wc -l`
		while [ $MacCount -lt $macs ];do
			MacCount=$((${MacCount}+1))
			mac=`iwinfo $dev3 assoclist | grep ..:..:..:..:..:.. | grep dBm | sed -n ${MacCount}p | awk '{print $1}'`
			rssi=`iwinfo $dev3 assoclist | grep ..:..:..:..:..:.. | grep dBm | sed -n ${MacCount}p | awk '{print $4}'`
			if [ $rssi -lt $Threshold3 ];then
				#if [ $devHide3 == 0 ];then iwpriv $dev3 hide_ssid 1;fi
				iwpriv $dev3 kickmac $mac
				echo [$(date +%F%n%T)]设备：`cat /tmp/dhcp.leases | grep -i $mac | awk '{print $4}'` IP: `cat /tmp/dhcp.leases | grep -i $mac | awk '{print $3}'` MAC：$mac 所在WIFI：`iwinfo $dev3 info | grep ESSID | sed 's/^.*: //g'` 信号强度：$rssi 低于已设置阈值 $Threshold3 ，已被踢出！ >> $logfile
				if [ $devSec == 2 ];then iwpriv $dev3 addmac_sec $mac
				else iwpriv $dev3 delmac_sec $mac;echo $mac >> "/tmp/${dev3}whitelist.tmp";fi
			fi
		done;MacCount=0
		if [ $devSec == 2 ];then bmacs=`iwpriv $dev3 getmac_sec`;bmacs=${bmacs#*:};else bmacs=`cat /tmp/${dev3}whitelist.tmp | head -n 1`>&nul;fi
		if [ "$bmacs" != "" ];then BMacTimeCount3=$((${BMacTimeCount3}+1));else BMacTimeCount3=0;fi
		if [ $BMacTimeCount3 == 11 ];then BMacTimeCount3=0
			#if [ $devHide3 == 0 ];then iwpriv $dev3 hide_ssid 0;fi
			if [ $devSec == 2 ];then
				while [ 0 -lt `iwpriv $dev3 getmac_sec | grep ..:..:..:..:..:.. | sed 's/^.*c://g' | wc -l` ];do
					bmacs=`iwpriv $dev3 getmac_sec | grep ..:..:..:..:..:.. | sed 's/^.*c://g' | sed -n 1p`
					iwpriv $dev3 delmac_sec $bmacs
				done
			else
				while [ 0 -lt `cat /tmp/${dev3}whitelist.tmp | wc -l` ];do
					bmacs=`cat /tmp/${dev3}whitelist.tmp | sed -n 1p`
					iwpriv $dev3 addmac_sec $bmacs
					sed -i '1d' /tmp/${dev3}whitelist.tmp
				done
				rm -Rf /tmp/${dev3}whitelist.tmp
			fi
		fi
	fi
	sleep 1
done
